<?php
class Product_categoryAction extends CommonAction {
	public $listmap=1;
	public $listorder="p.sort";
	public $pagezise=20;
    public function index(){
		setcookie('product_id', '', time()-1, '/');
		$this->ajax = false;
		if(isset($_GET['displayall_'.__SITEID__])){
			setcookie('displayall_'.__SITEID__, 1, time() + 3600, '/');
		}
		$this->listmap .= " and p.status > 0 and p.site_id=".__SITEID__;
		if($this->_get('keywords')){
			$this->listmap .= " and p.title like '%".$this->_get('keywords')."%'";
			$this->keywords = $this->_get('keywords');
			$this->assign('ajax',true);
		}
		if($this->_get('minprice')){
			$this->listmap .= " and p.price > '".$this->_get('minprice')."'";
		}
		
		if($this->_get('maxprice')){
			$this->listmap .= " and p.price < '".$this->_get('maxprice')."'";
		}
		//排序
		if($this->_get('sortfield')){
			$this->listorder = "p.".$this->_get('sortfield')." ".$this->_get('sorttype');
			$this->sortfield = $this->_get('sortfield');
			$this->sorttype = $this->_get('sorttype');
		}

		if($this->_get('group_type')){
			$s_limit=' and type = '.$_GET['group_type'];
			$supplier= M('Supplier') -> where('site_id= 143 '.$s_limit) -> getfield('id',true);
			$supplier=implode(',',$supplier);
			$this->listmap .= ' and p.supplier_id in('.$supplier.')';
		}

		if($this->_get('id')){
			$this->_index($this->_get('id'));
		}else{ 
			$this->_index(0);
		}



		$template=array();
		foreach(($this->pagenav) as $k=>$v){
			if($v['page_list_id']!=0){
				$template['page_list_id']=$v['page_list_id'];
			}
			if($v['page_list_header_id']!=0){
				$template['page_list_header_id']=$v['page_list_header_id'];
			}
			if($v['page_list_footer_id']!=0){
				$template['page_list_footer_id']=$v['page_list_footer_id'];
			}
			if($v['page_view_id']!=0){
				$template['page_view_id']=$v['page_view_id'];
			}
			if($v['page_view_header_id']!=0){
				$template['page_view_header_id']=$v['page_view_header_id'];
			}
			if($v['page_view_footer_id']!=0){
				$template['page_view_footer_id']=$v['page_view_footer_id'];
			}
		}
		$ModulesDB = D('Modules');
		$this->initView();
		$this->getModulesByID($this->view, $ModulesDB);
		$request =$this->view -> fetch('Public:header');
		$request .= $ModulesDB->getListToString($template['page_list_header_id'],$this->view);
		$request .= $ModulesDB->getListToString($template['page_list_id'],$this->view);
		$request .= $ModulesDB->getListToString($template['page_list_footer_id'],$this->view);
		$request .= $this->view -> fetch('Public:footer');
		$request .= $this->view -> fetch('Public:footerscript');
		if(isset($_GET['isajaxlist'])){
			exit(json_encode(array('empty' => 0,'tbody'=>$request,'dump'=>$_REQUEST,'statu'=>1)));
		}
		exit($request);
		
    }
	
	public function _index($id){
		
			$this->assign('cid',$id);
			
			
			$csql = M('Category_goods') -> where(array('category_id' => $id)) -> Field('product_id as id') -> buildSql();
			
			
			
			$Product_categoryModel=D("Product_category");
			if($id > 0){
				$ids=$Product_categoryModel->getAllIDBypid($id, __SITEID__);
				if(isset($_REQUEST['cateself']) && intval($_REQUEST['cateself']) > 0){
					$this->listmap.=' and (p.id in('.$csql.') or p.category_id = '.$id.')';
				}else{
					$this->listmap.=' and (p.category_id in('.implode(',',$ids).') or p.id in('.$csql.'))';
				}
			}
			
			 
			
			//下级分类,如果没有下级,显示同级
			$subCategory = $Product_categoryModel -> where(array('status' => array('gt', 0), 'pid' => $id,'site_id' => __SITEID__)) -> order('sort') -> select();
			
			$this->subCategory = $subCategory;
			$curcate = $Product_categoryModel -> where(array('id' => $id)) -> find();
			
			
			if($curcate['levelBuy'] > 0){
				if($curcate['levelBuy'] > $this->memberInfo['level']){
					$this->error('当前等级无法查看该专区');exit;
				}
			}
			
			
			//没有子级分类,显示同级
			if(!is_array($subCategory)){
				
				//$subCategory = $Product_categoryModel -> where(array('pid' => $curcate['pid'],'site_id' => __SITEID__)) -> select();
			}
			//dump($subCategory);
			if(!$this->_get('keywords')){
			$this->assign('subcate', $subCategory);
			}
			$this -> assign ('curcate', $curcate);
			//页面导航
			$pagenav = $Product_categoryModel->getParentCate($id,__SITEID__);
			$pagenav1[]['title'] = '产品分类';
			foreach($pagenav as $v){
				$pagenav1[] = $v;
			}
			$this->assign('pagenav',$pagenav1);
			
			//模块页面
			
			
			
			//模板风格
			//$typeid = $Product_categoryModel->getTypeById($id,__SITEID__);//当前类型ID
			//通过typeid查询当前类型
			//$template = M('Type') -> where(array('id' => $typeid)) -> find();
			$attrList=$Product_categoryModel->getAttrList($id,__SITEID__);
			$this->assign('attrList',$attrList);
			//当前选中的属性
			$attrArray=array();
			$attr_valArray=array();
			if(isset($_GET['attrvalRe'])){
				$attrval=$this->_get('attrvalRe');
				$attrval=explode(';',$attrval);
				$this->assign('curattrval',$attrval);
				//构造两个筛选条件dump($list);属性ID集合与值ID集合
				foreach($attrval as $k=>$v){
					$a=explode('_',$v);
					if(count($a)>0&&intval($a[0])>0&&intval($a[1])>0){
						$attrArray[]=$a[0];
					    $attr_valArray[]=$a[1];
					}
				}
			}
			//如果存在属性筛选//读取商品ID或者联合商品表直接查询商品
			import('ORG.Util.Page');
			if(count($attrArray)>0){
				$product_attrModel=M('Product_attr');
				$this -> listmap .= " and a.attr_id in(".implode(',',$attrArray).") and a.attr_val_id in(".implode(',',$attr_valArray).")";
				//计算总数
				
				$count = $product_attrModel->alias('a')->join("left join __PRODUCT__ as p on p.id=a.product_id")->where($this->listmap)->group('a.product_id')->having("count(*)=".count($attrArray))->Field("p.*")->select();
				$count=count($count);
				//生成分页
				$page = new Page($count, $this->pagezise);
				$list = $product_attrModel->alias('a')->join("left join __PRODUCT__ as p on p.id=a.product_id")->where($this->listmap)->group('a.product_id')->having("count(*)=".count($attrArray))->Field("p.*")->limit($page->firstRow.',
'.$page->listRows)->select();
                $pages = $page->show();
				//扩展
				foreach($list as $k=>$v){
				    $list[$k]['extends'] = getProductExtends($v['id']);
					if(defined('__PAGERELATEID__')){
						//替换不同版本的信息
						$siterelate = siterelate($v['id'], __PAGERELATEID__, 'Product');
						$list[$k] = array_merge($list[$k], $siterelate);
					}
			    }
				
				$this->assign('list',$list);
				$this->assign('pages',$pages);
			}else{
				//没有属性筛选
				//$template['template'] = 'index';
				$productModel=M('Product');
				$count=$productModel->alias('p')->where($this->listmap)->count();
				$page = new Page($count, $this->pagezise);
				$list = $productModel->alias('p')->where($this->listmap)->order($this->listorder)->limit($page->firstRow.','.$page->listRows)->select();
				$pages = $page->show();
				//扩展
				foreach($list as $k=>$v){
				    $list[$k]['extends'] = getProductExtends($v['id']);
					if(defined('__PAGERELATEID__')){
						//替换不同版本的信息
						$siterelate = siterelate($v['id'], __PAGERELATEID__, 'Product');
						$list[$k] = array_merge($list[$k], $siterelate);
					}
			    }
				//echo $productModel->getLastSql();
				
				
				
				
				
				$this->assign('list',$list);
				
				$this->assign('pages',$pages);
			}
			
			
			
	}
	
	
	
	
	public function _indexajax($id){
		
		
		
		//$Action->cid = $id;
		
			$this->assign('cid',$id);
			
			
			//$csql = M('Category_goods') -> where(array('category_id' => $id)) -> Field('product_id as id') -> buildSql();
			
			
			
			$Product_categoryModel=D("Product_category");
			if($id > 0){
				$ids=$Product_categoryModel->getAllIDBypid($id, __SITEID__);
				
					$this->listmap.=' and (p.category_id in('.$id.'))';

			}
			
			
			
			//下级分类,如果没有下级,显示同级
			$subCategory = $Product_categoryModel -> where(array('pid' => $id,'site_id' => __SITEID__)) -> order('sort') -> select();
			
			$this->subCategory = $subCategory;
			$curcate = $Product_categoryModel -> where(array('id' => $id)) -> find();
			
			//没有子级分类,显示同级
			if(!is_array($subCategory)){
				
				//$subCategory = $Product_categoryModel -> where(array('pid' => $curcate['pid'],'site_id' => __SITEID__)) -> select();
			}
			//dump($subCategory);
			if(!$this->_get('keywords')){
			$this->assign('subcate', $subCategory);
			}
			$this -> assign ('curcate', $curcate);
			//页面导航
			$pagenav = $Product_categoryModel->getParentCate($id,__SITEID__);
			$pagenav1[]['title'] = '产品分类';
			foreach($pagenav as $v){
				$pagenav1[] = $v;
			}
			$this->assign('pagenav',$pagenav1);
			
			//模块页面
			
			
			
			//模板风格
			//$typeid = $Product_categoryModel->getTypeById($id,__SITEID__);//当前类型ID
			//通过typeid查询当前类型
			//$template = M('Type') -> where(array('id' => $typeid)) -> find();
			$attrList=$Product_categoryModel->getAttrList($id,__SITEID__);
			$this->assign('attrList',$attrList);
			//当前选中的属性
			$attrArray=array();
			$attr_valArray=array();
			if(isset($_GET['attrvalRe'])){
				$attrval=$this->_get('attrvalRe');
				$attrval=explode(';',$attrval);
				$this->assign('curattrval',$attrval);
				//构造两个筛选条件dump($list);属性ID集合与值ID集合
				foreach($attrval as $k=>$v){
					$a=explode('_',$v);
					if(count($a)>0&&intval($a[0])>0&&intval($a[1])>0){
						$attrArray[]=$a[0];
					    $attr_valArray[]=$a[1];
					}
				}
			}
			//如果存在属性筛选//读取商品ID或者联合商品表直接查询商品
			import('ORG.Util.Page');
			if(count($attrArray)>0){
				$product_attrModel=M('Product_attr');
				$this -> listmap .= " and a.attr_id in(".implode(',',$attrArray).") and a.attr_val_id in(".implode(',',$attr_valArray).")";
				//计算总数
				
				$count = $product_attrModel->alias('a')->join("left join __PRODUCT__ as p on p.id=a.product_id")->where($this->listmap)->group('a.product_id')->having("count(*)=".count($attrArray))->Field("p.*")->select();
				$count=count($count);
				//生成分页
				$page = new Page($count, $this->pagezise);
				$list = $product_attrModel->alias('a')->join("left join __PRODUCT__ as p on p.id=a.product_id")->where($this->listmap)->group('a.product_id')->having("count(*)=".count($attrArray))->Field("p.*")->limit($page->firstRow.',
'.$page->listRows)->select();
                $pages = $page->show();
				//扩展
				foreach($list as $k=>$v){
				    $list[$k]['extends'] = getProductExtends($v['id']);
			    }
				
				$this->assign('list',$list);
				$this->assign('pages',$pages);
			}else{
				//没有属性筛选
				//$template['template'] = 'index';
				$productModel=M('Product');
				$count=$productModel->alias('p')->where($this->listmap)->count();
				$page = new Page($count, $this->pagezise);
				$list = $productModel->alias('p')->where($this->listmap)->order($this->listorder)->limit($page->firstRow.','.$page->listRows)->select();
				$pages = $page->show();
				//扩展
				foreach($list as $k=>$v){
				    $list[$k]['extends'] = getProductExtends($v['id']);
			    }
				//echo $productModel->getLastSql();
				
				
				
				//echo $productModel -> getLastSql();
				
				$this->assign('list',$list);
				
				$this->assign('pages',$pages);
			}
			
			
			
			
	}
}